package com.liuwei.namespaceServer;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;

public class NamespaceAndRoomChatLauncher {

	public static void main(String[] args) throws InterruptedException {

		Configuration config = new Configuration();
		config.setHostname("localhost");
		config.setPort(9092);

		final SocketIOServer server = new SocketIOServer(config);
		final SocketIONamespace chat1namespace = server.addNamespace("/chat1");

		chat1namespace.addEventListener("message", ChatObject.class, new DataListener<ChatObject>() {
			@Override
			public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
				client.joinRoom(data.getRoom());// 添加到room
				chat1namespace.getRoomOperations(data.getRoom()).sendEvent("message", data);
			}
		});

		final SocketIONamespace chat2namespace = server.addNamespace("/chat2");
		chat2namespace.addEventListener("message", ChatObject.class, new DataListener<ChatObject>() {
			@Override
			public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
				client.joinRoom(data.getRoom());// 添加到room
				chat2namespace.getRoomOperations(data.getRoom()).sendEvent("message", data);
			}
		});
		server.start();

		Thread.sleep(Integer.MAX_VALUE);

		server.stop();
	}

}